package com.lottery.material.controller.portal.circle;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.jedis.JedisClient;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.service.portal.circle.CircleMemberInfoService;
import com.lottery.material.service.portal.user.PortalUserInfoService;
import com.lottery.material.util.ParameterUtil;
import com.lottery.material.util.ResponseResult;
import com.lottery.material.vo.portal.circle.MemberCircleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "圈子相关接口", tags = { "圈子相关接口" })
@Controller
@RequestMapping("/portal/circle")
@Slf4j
public class CircleController extends BaseController {


    @Autowired
    @Qualifier("jedisClientCluster")
    private JedisClient jedisClient;

    @Autowired
    private PortalUserInfoService portalUserInfoService;

    @Autowired
    CircleMemberInfoService circleMemberInfoService;

    @ApiOperation(value="置顶圈子列表接口", notes="关注的圈子列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户唯一标识", required = true, dataType = "String"),
            @ApiImplicitParam(name = "accessToken", value = "访问令牌（登录的用户必传）", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/getTopCircleList" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getTopCircleList(HttpServletRequest request) {
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }
        ParameterUtil.checkLogin(reqParamMap,jedisClient, portalUserInfoService);

        ResponseResult x = ParameterUtil.checkSign(reqParamMap);
        if (x != null) return x;

        List<MemberCircleVo> memberCircleVos = circleMemberInfoService.findTopCircleList(reqParamMap.get("userId"));

        return ResponseResult.buildSuccess(memberCircleVos);
    }

    @ApiOperation(value="我的圈子列表接口", notes="关注的圈子列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户唯一标识", required = true, dataType = "String"),
            @ApiImplicitParam(name = "accessToken", value = "访问令牌（登录的用户必传）", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/getMyCircleList" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getMyCircleList(HttpServletRequest request) {

        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }
        ParameterUtil.checkLogin(reqParamMap,jedisClient, portalUserInfoService);

        ResponseResult x = ParameterUtil.checkSign(reqParamMap);
        if (x != null) return x;

        List<MemberCircleVo> memberCircleVos = circleMemberInfoService.findMyCircleList(reqParamMap.get("userId"));

        return ResponseResult.buildSuccess(memberCircleVos);
    }


    @ApiOperation(value="置顶/取消圈子接口", notes="置顶/取消圈子接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户唯一标识", required = true, dataType = "String"),
            @ApiImplicitParam(name = "accessToken", value = "访问令牌（登录的用户必传）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "circleId", value = "圈子id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "isTop", value = "是否置顶（1：取消置顶；2：置顶）", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/setTopCircle" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult setTopCircle(HttpServletRequest request) {
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }
        ParameterUtil.checkLogin(reqParamMap,jedisClient, portalUserInfoService);

        ResponseResult x = ParameterUtil.checkSign(reqParamMap);
        if (x != null) return x;

        String circleId = reqParamMap.get("circleId");
        if (StringUtils.isBlank(circleId)) {
            return ResponseResult.buildResult(ResultEnum.PARAMETER_BLANK_ERROR.getCode(), String.format(ResultEnum.PARAMETER_BLANK_ERROR.getMessage(),"circleId"));
        }

        String isTop = reqParamMap.get("isTop");
        if (StringUtils.isBlank(isTop)) {
            return ResponseResult.buildResult(ResultEnum.PARAMETER_BLANK_ERROR.getCode(), String.format(ResultEnum.PARAMETER_BLANK_ERROR.getMessage(),"isTop"));
        }

        Integer i = circleMemberInfoService.setTopCircle(reqParamMap);
        switch(i){
            case 1 ://取消置顶成功
                return ResponseResult.buildResult(ResultEnum.SET_CIRCLE_UNTOP_SUCCESS);
            case 2 ://置顶成功
                return ResponseResult.buildResult(ResultEnum.SET_CIRCLE_TOP_SUCCESS);
            case 3 ://已经取消
                return ResponseResult.buildResult(ResultEnum.SET_CIRCLE_TOP_ALREADYCANCELD);
            case 4 ://已置顶
                return ResponseResult.buildResult(ResultEnum.SET_CIRCLE_TOP_ALREADYSETTOP);
            default://请求失败
                return ResponseResult.buildResult(ResultEnum.SET_CIRCLE_TOP_FAIL);
        }
    }

}
